package com.zjw.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjw.shortlink.project.common.convention.result.Result;
import com.zjw.shortlink.project.common.convention.result.Results;
import com.zjw.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import com.zjw.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.zjw.shortlink.project.dto.req.ShortLinkPageReqDTO;
import com.zjw.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import com.zjw.shortlink.project.dto.resp.*;
import com.zjw.shortlink.project.handler.CustomBlockHandler;
import com.zjw.shortlink.project.service.ShortLinkService;
import com.zjw.shortlink.project.toolkit.EasyExcelWebUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;

    @GetMapping("/{short-url}")
    public void restoreUrl(@PathVariable("short-url") String shortUrl, HttpServletRequest request, HttpServletResponse response) {
        shortLinkService.restoreUrl(shortUrl, request, response);
    }

    @PostMapping("/api/short-link/v1/create")
    @SentinelResource(
            value = "create_shortlink",
            blockHandler = "handleCreateShortLink",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO requestParams) {
        return Results.success(shortLinkService.createShortLink(requestParams));
    }

    @PostMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(@RequestBody ShortLinkPageReqDTO requestParams) {
        return Results.success(shortLinkService.pageShortLink(requestParams));
    }

    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(@RequestParam("requestParams") List<String> requestParams) {
        return Results.success(shortLinkService.listGroupShortLinkCount(requestParams));
    }

    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParams) {
        shortLinkService.updateShortLink(requestParams);
        return Results.success();
    }

    @PostMapping("/api/short-link/admin/v1/create/batch")
    public void batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam, HttpServletResponse response) {
        ShortLinkBatchCreateRespDTO shortLinkBatchCreateRespDTO = shortLinkService.batchCreateShortLink(requestParam);
        if(shortLinkBatchCreateRespDTO.getTotal() > 0) {
            EasyExcelWebUtil.write(response, "短链接批量生成结果", ShortLinkBaseInfoRespDTO.class, shortLinkBatchCreateRespDTO.getBaseLinkInfos());
        }
    }
}
